﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Signup.ascx.cs" Inherits="UserControls_Signup" %>
<style type="text/css">
    .tblSignup
    {
        width: 450px;
        text-align: left;
        font-family: Lucida Grande, Geneva, Arial, Verdana, sans-serif;
        font-size: 14px;
        margin-top: 10px;
        margin-bottom: 10px;
        border: solid 1px #999999;
    }
    #txtAddress
    {
        width: 247px;
    }
    .Header
    {
        background-color: #333333;
        color: White;
        font-weight: bolder;
        font-size: large;
        padding-left: 20px;
    }
</style>
<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $('#btnRegister').button();
        var Mode = $f('<%=hdnMode.ClientID%>').value;
        SetMode(Mode);
        
        if(Mode=="EditProfile")
        {
            fillPage();
        }
    });
    
   
    
    function fillPage()
    {
    
        var contact=eval('('+$f('<%=hdnContact.ClientID%>').value+')');
        
        $f('<%=txtFirstname.ClientID%>').value=contact.FirstName==null?'':contact.FirstName;
        $f('txtLastname').value= contact.LastName==null?'':contact.LastName;
        $f('<%=txtEmail.ClientID%>').value=contact.EmailAddress==null?'':contact.EmailAddress;
        $f('txtTelephone').value=contact.TelNumber==null?'':contact.TelNumber;
        $f('txtCellphone').value=contact.CellNumber==null?'':contact.CellNumber;
        $f('txtFax').value=contact.FaxNumber==null?'':contact.FaxNumber;
        debugger;
        for (var i = 0; $f('<%=drpPostalCode.ClientID%>').options.length < i; i++)
        {
            if ($f('<%=drpPostalCode.ClientID%>').options[i].text == contact.PostalCode){
                $f('<%=drpPostalCode.ClientID%>').value = i;
            }
        }
        $f('txtStreet').value=contact.Street==null?'':contact.Street;
        $f('txtHouseNumber').value=contact.HomeNumber==null?'':contact.HomeNumber;
        $f('txtStair').value=contact.Stair==null?'':contact.Stair;
        $f('txtFloor').value=contact.Floor==null?'':contact.Floor;
        $f('txtDoor').value=contact.Door==null?'':contact.Door;
        
    }
    
    function SetMode(Mode) {
        //Mode=Contact for save Contact
        //Mode=SignUp for save User and Contact
        

        if (Mode == 'Contact') {
            $f('lbltitle').innerText='Registrieren Kontakt';
            $('.SignupRow').each(function() {
                this.style.display = 'none';
            });
        }
        else
            if (Mode == 'SignUp') {
                $f('lbltitle').innerText='Registrieren User';
                $('.SignupRow').each(function() {
                    this.style.display = '';
                });
        }
        else
            if(Mode=='EditProfile')
            {
                $f('lbltitle').innerText='Profil bearbeiten';
            }
    }

    function PrepareboContact() {
    debugger;
    
        var BOContact = new Object();
        BOContact.FirstName = $f('<%=txtFirstname.ClientID%>').value;
        BOContact.LastName = Isundefined($f('txtLastname').value, null);
        BOContact.EmailAddress = $f('<%=txtEmail.ClientID%>').value;
        BOContact.TelNumber = Isundefined($f('txtTelephone').value, null);
        BOContact.CellNumber = Isundefined($f('txtCellphone').value, null);
        BOContact.FaxNumber = Isundefined($f('txtFax').value, null);
        BOContact.PostalCode = $f('<%=drpPostalCode.ClientID%>').options[$f('<%=drpPostalCode.ClientID%>').selectedIndex].text;
        BOContact.Street = Isundefined($f('txtStreet').value,null);
        BOContact.HomeNumber = Isundefined($f('txtHouseNumber').value, null);
        BOContact.Stair = Isundefined($f('txtStair').value, null);
        BOContact.Floor = Isundefined($f('txtFloor').value, null);
        BOContact.Door = Isundefined($f('txtDoor').value, null);
        
        return JSON.stringify(BOContact);
    }

    function PrepareboUser() {
        var BOUser = new Object();
        BOUser.LoginID = $f('<%=txtEmail.ClientID%>').value;
        BOUser.Password = $f('<%=txtPassword.ClientID%>').value;

        return JSON.stringify(BOUser);
    }

    function SaveClicked() {
        if (Page_ClientValidate()) {
            var Mode = $f('<%=hdnMode.ClientID%>').value;
            
            if (Mode == 'Contact') {
                var ClientData = new Array();
                ClientData.push(Mode);
                ClientData.push(PrepareboContact());
                signupCallServer(JSON.stringify(ClientData));
            } else
                if (Mode == 'SignUp') {
                    var ClientData = new Array();
                    ClientData.push(Mode);
                    ClientData.push(PrepareboContact());
                    ClientData.push(PrepareboUser());
                    signupCallServer(JSON.stringify(ClientData));
            } 
            else
                if(Mode=='EditProfile'){
                    var ClientData = new Array();
                    ClientData.push(Mode);
                    ClientData.push(PrepareboContact())
                    ClientData.push(PrepareboUser());
                    signupCallServer(JSON.stringify(ClientData));
                }
        }
    }
    
     function showError(msg)
    {
        $('#spnMessage')
        .removeClass('success')
        .addClass('error')
        .html(msg);
        
    }
    function showSuccess(msg)
    {
        $('#spnMessage')
        .removeClass('error')
        .addClass('success')
        .html(msg);
    }
    function signupReceive(data) {
        if (data != 'undefined') {
            var ServerData=eval('('+data+')');
            var Command=ServerData[0];
            var state=ServerData[1];
            if (Command == 'Contact') {
                if (state == 'Success') { 
                var ContactID=ServerData[2];
                if(typeof(<%=ClientEventafterInsert%>)=='function')
                    eval('('+<%=ClientEventafterInsert%>+'('+ContactID+')'+')');
                }
            }
            if (Command == 'SignUp') { 
            if (state == 'Success') { 
                var ContactID=ServerData[2];
                var UserID=ServerData[3];
                showSuccess("Ihre registartion war erfolgreich");
                if(typeof(<%=ClientEventafterInsert%>)=='function')
                    eval('('+<%=ClientEventafterInsert%>+'('+ContactID+','+UserID+')'+')');
                    
                }
             else{
                  showError(ServerData[2]);
             }
            }
            if(Command=='EditProfile')
            {
                if(state=='Success')
                {
                    showSuccess("Ihre registartion war erfolgreich");
                }
                else
                {
                    showError(ServerData[2]);
                }
            }
        }
    }

</script>
<asp:HiddenField ID="hdnContact" runat="server" />
<asp:HiddenField ID="hdnMode" runat="server" Value="Contact" />
<table class="tblSignup ui-corner-all" cellpadding="2" align="center">
    <tr>
        <td colspan="2" class="Header">
            <label id="lbltitle">
            </label>
        </td>
    </tr>
    <tr>
        <td>
            Vorname
        </td>
        <td>
            <input type="text" id="txtFirstname" class="Textbox" runat="server" />
            <asp:RequiredFieldValidator ID="vldFirstName" runat="server" ErrorMessage="*" ValidationGroup="regSignUp"
                ControlToValidate="txtFirstname"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Nachname
        </td>
        <td>
            <input type="text" id="txtLastname" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            Telefon
        </td>
        <td>
            <input type="text" id="txtTelephone" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            Cellphone
        </td>
        <td>
            <input type="text" id="txtCellphone" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            Fax
        </td>
        <td>
            <input type="text" id="txtFax" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            PLZ
        </td>
        <td>
            <asp:DropDownList ID="drpPostalCode" runat="server">
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            Straße
        </td>
        <td>
            <input type="text" id="txtStreet" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            Hausnr.
        </td>
        <td>
            <input type="text" id="txtHouseNumber" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            Stiege
        </td>
        <td>
            <input type="text" id="txtStair" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            Etage
        </td>
        <td>
            <input type="text" id="txtFloor" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            Tür
        </td>
        <td>
            <input type="text" id="txtDoor" class="Textbox" />
        </td>
    </tr>
    <tr>
        <td>
            E-Mail
        </td>
        <td>
            <input type="text" id="txtEmail" class="Textbox" runat="server" /><asp:RequiredFieldValidator
                ID="vldEmail" runat="server" ErrorMessage="*" ValidationGroup="regSignUp" ControlToValidate="txtEmail"></asp:RequiredFieldValidator><asp:RegularExpressionValidator
                    ID="vldEmailRegx" runat="server" ControlToValidate="txtEmail" ErrorMessage="Example:a@b.c"
                    ValidationGroup="regSignUp" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr class="SignupRow">
        <td>
            Ihr neues Kennwort
        </td>
        <td>
            <input type="password" id="txtPassword" class="Textbox" runat="server" />
        </td>
    </tr>
    <tr class="SignupRow">
        <td>
            Kennwort wiederholen
        </td>
        <td>
            <input type="password" id="txtConfirmPassword" class="Textbox" runat="server" />
            <asp:CompareValidator ID="vldConfirmPassword" runat="server" ErrorMessage="They are not equal"
                ControlToCompare="txtPassword" ControlToValidate="txtConfirmPassword" ValidationGroup="regSignUp"></asp:CompareValidator>
        </td>
    </tr>
    <tr>
        <td>
            <input id="btnRegister" type="button" value="Weiter" validationgroup="regSignUp"
                onclick="SaveClicked()" />
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <span id="spnMessage" class=""></span>
        </td>
    </tr>
</table>
